<?php

namespace base\admin\service;

use app\admin\service\node\Node;

class NodeServiceBase
{
    /**
     * 获取节点服务
     * @return array
     * @throws \Doctrine\Common\Annotations\AnnotationException
     * @throws \ReflectionException
     */
    public function getNodelist()
    {
        $basePath = base_path() . 'admin' . DIRECTORY_SEPARATOR . 'controller';
        $baseNamespace = "app\admin\controller";

        $nodeList = (new Node($basePath, $baseNamespace))
            ->getNodelist();

        return $nodeList;
    }
    public function getNodeParis()
    {
        $node_list = $this->getNodelist();
        return array_column($node_list, null, 'node');
    }

    public function getNodeCollection() 
    {
        $nodeList = $this->getNodelist();

        $nodeCollection = collect($nodeList);

        return $nodeCollection;
    }

    /**
     * 获取节点服务
     * @return array
     * @throws \Doctrine\Common\Annotations\AnnotationException
     * @throws \ReflectionException
     */
    public function getNodeTree()
    {
        $nodelList = $this->getNodelist();

        $newNodeList = [];
        foreach ($nodelList as $vo) {
            if ($vo['type'] == 1) {
                if (!isset($newNodeList[$vo['module']])) {
                    $newNodeList[$vo['module']] = [
                        'title' => $vo['module'],
                        'children' => [],
                    ];
                }

                $vo['title'] = "{$vo['title']}";
                $children = [];
                foreach ($nodelList as $v) {
                    if ($v['type'] == 2 && strpos($v['node'], $vo['node'] . '/') !== false) {
                        $v['title'] = "{$v['title']}";
                        $children[] = $v;
                    }
                }
                !empty($children) && $vo['children'] = $children;
                $newNodeList[$vo['module']]['children'][] = $vo;
            }
        }
        $newNodeList = array_values($newNodeList);
        array_unshift($newNodeList, ['title' => '全部', 'children' => []]);

        return $newNodeList;
    }
}
